package com.bigdata.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.core.page.TableDataInfo;
import com.bigdata.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.bigdata.service.domain.bo.UserFeedbackBo;
import com.bigdata.service.domain.vo.UserFeedbackVo;
import com.bigdata.service.domain.UserFeedback;
import com.bigdata.service.mapper.UserFeedbackMapper;
import com.bigdata.service.service.IUserFeedbackService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户反馈Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-22
 */
@RequiredArgsConstructor
@Service
public class UserFeedbackServiceImpl implements IUserFeedbackService {

    private final UserFeedbackMapper baseMapper;

    /**
     * 查询用户反馈
     */
    @Override
    public UserFeedbackVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户反馈列表
     */
    @Override
    public TableDataInfo<UserFeedbackVo> queryPageList(UserFeedbackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserFeedback> lqw = buildQueryWrapper(bo);
        Page<UserFeedbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户反馈列表
     */
    @Override
    public List<UserFeedbackVo> queryList(UserFeedbackBo bo) {
        LambdaQueryWrapper<UserFeedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserFeedback> buildQueryWrapper(UserFeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserFeedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserFeedback::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getFeedbackType()), UserFeedback::getFeedbackType, bo.getFeedbackType());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), UserFeedback::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getFeedbackStatus()), UserFeedback::getFeedbackStatus, bo.getFeedbackStatus());
        lqw.like(StringUtils.isNotBlank(bo.getCreateBy()), UserFeedback::getCreateBy, bo.getCreateBy());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), UserFeedback::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getUserPhone()), UserFeedback::getUserPhone, bo.getUserPhone());
        lqw.orderByDesc(UserFeedback::getCreateTime);
        return lqw;
    }

    /**
     * 新增用户反馈
     */
    @Override
    public Boolean insertByBo(UserFeedbackBo bo) {
        UserFeedback add = BeanUtil.toBean(bo, UserFeedback.class);

        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户反馈
     */
    @Override
    public Boolean updateByBo(UserFeedbackBo bo) {
        UserFeedback update = BeanUtil.toBean(bo, UserFeedback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserFeedback entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户反馈
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
